﻿Imports Mysoft.Map.Application
Imports System.Data
Imports System.Xml
Imports Mysoft.Map.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.IO
Imports Mysoft.ZipLib.Zip
Imports Mysoft.ZipLib.Checksums
Imports Mysoft.Map.Application.Security.User
Imports System.Text

Public Class CwjkZt



#Region "保存账套"
	'保存财务接口账套信息
	Public Shared Function SaveCwjkZt(ByVal DataXml As String, ByVal UserDataXml As String) As String
		Dim strSql As String
		Dim oid, strKeyValue, strApplication, strJzMode, strIsPos, strOldIsPos, strTemplateFileName, strNewRemarks, strBUGUID As String
		Dim xmlDom As New XmlDocument
		strTemplateFileName = ""
		xmlDom.LoadXml(DataXml)
		strKeyValue = xmlDom.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText		'新增、修改的标识
		strApplication = xmlDom.SelectSingleNode("/p_CwjkCwzt/Application").InnerText
		strJzMode = xmlDom.SelectSingleNode("/p_CwjkCwzt/JzMode").InnerText
		strIsPos = xmlDom.SelectSingleNode("/p_CwjkCwzt/IsPOS").InnerText
		strNewRemarks = xmlDom.SelectSingleNode("/p_CwjkCwzt/Remarks").InnerText
		strBUGUID = xmlDom.SelectSingleNode("/p_CwjkCwzt/BUGUID").InnerText
		'新增时，查找模板文件名称，判断模板文件是否存在
		If strKeyValue = "" Then
			strSql = "SELECT TemplateFile  FROM p_CwjkTemplateFile WHERE Application='" & strApplication & "' AND JzMode = '" & strJzMode & "' AND IsPos = '" & strIsPos & "'"
			strTemplateFileName = MyDB.GetDataItemString(strSql)
			If Not CheckTemplateIsExists(strTemplateFileName) Then
				Return "<xml result=""false"" errormessage=""标准模板文件不存在，不允许保存！""></xml>"
			End If
		End If    

		oid = MyDB.SaveXml(DataXml)

		'保存成功
		If oid <> "" Then
			'新增保存账套模板中数据
			If strKeyValue.Length = 0 Then
				Return SaveCwjkZtOther(oid, strBUGUID, strApplication, strNewRemarks, strTemplateFileName)
			Else '修改
				Dim xmlUser As XmlDocument = New XmlDocument()
				If UserDataXml <> "" Then
					xmlUser.LoadXml(UserDataXml)
					Dim node = xmlUser.SelectSingleNode("/items/item")
					Dim isChangeIsPos As String = node.Attributes("IsChangeIsPost").Value
					strOldIsPos = node.Attributes("OldIsPOS").Value
					'如果修改“是否存在POS机费用”，则需更新	“凭证分录规则”
					If isChangeIsPos = 1 Then
						strSql = "UPDATE p_CwjkPzEntryRule SET IsDisable = '" & strOldIsPos & "' WHERE CwztGUID = '" & oid & "' AND PzName =  'POS机' ;"
						'预付模式
						If strJzMode = 0 Then
							'无POS机费用
							If strIsPos = 0 Then
								strSql &= "UPDATE p_CwjkPzEntryRule SET KmSqlFilterRule = '( GetForm=''银行''  AND VouchType=''收款单'')' WHERE CwztGUID= '" & oid & "' AND PzName = '银行/支票' ;"
							Else '有POS机费用
								strSql &= "UPDATE p_CwjkPzEntryRule SET KmSqlFilterRule = '( GetForm=''银行'' AND YhPayform <> ''POS机'' AND VouchType=''收款单'')' WHERE CwztGUID= '" & oid & "' AND PzName = '银行/支票' ;"
							End If
						Else
							'无POS机费用
                            If strIsPos = 0 Then
                                strSql &= "UPDATE p_CwjkPzEntryRule SET KmSqlFilterRule = '( GetForm=''银行''  AND VouchType=''收款单'')' WHERE CwztGUID= '" & oid & "' AND GroupName='' AND PzName = '银行/支票' ;"
                                strSql &= "UPDATE p_CwjkPzEntryRule SET KmSqlFilterRule = '((JzStatus<>1 OR (JzStatus=1 AND KpDate <= JzDate)) AND GetForm=''银行'' AND VouchType=''收款单'')' WHERE CwztGUID= '" & oid & "' AND GroupName='结转前' AND  PzName = '银行/支票' ;"
                                strSql &= "UPDATE p_CwjkPzEntryRule SET KmSqlFilterRule = '(JzStatus=1 AND KpDate > JzDate AND GetForm=''银行'' AND VouchType=''收款单'')' WHERE CwztGUID= '" & oid & "' AND GroupName='结转后' AND  PzName = '银行/支票' ;"
                            Else '有POS机费用
                                strSql &= "UPDATE p_CwjkPzEntryRule SET KmSqlFilterRule = '( GetForm=''银行'' AND YhPayform <> ''POS机'' AND VouchType=''收款单'')' WHERE CwztGUID= '" & oid & "' AND GroupName='' AND PzName = '银行/支票' ;"
                                strSql &= "UPDATE p_CwjkPzEntryRule SET KmSqlFilterRule = '((JzStatus<>1 OR (JzStatus=1 AND KpDate <= JzDate)) AND GetForm=''银行'' AND YhPayform <> ''POS机'' AND VouchType=''收款单'')' WHERE CwztGUID= '" & oid & "' AND GroupName='结转前' AND  PzName = '银行/支票' ;"
                                strSql &= "UPDATE p_CwjkPzEntryRule SET KmSqlFilterRule = '(JzStatus=1 AND KpDate > JzDate AND GetForm=''银行'' AND YhPayform <> ''POS机'' AND VouchType=''收款单'')' WHERE CwztGUID= '" & oid & "' AND GroupName='结转后' AND  PzName = '银行/支票' ;"
                            End If
                        End If
                        If strApplication = "0101" Then
                            If strIsPos = 0 Then
                                strSql &= " UPDATE p_CwjkOperType SET IsDisable=0 WHERE CwztGUID = '" & oid & "' AND HierarchyCode = '006.002';"
                                strSql &= " UPDATE p_CwjkOperType SET IsDisable=0 WHERE CwztGUID = '" & oid & "' AND HierarchyCode = '006';"
                            Else
                                strSql &= " UPDATE p_CwjkOperType SET IsDisable=1 WHERE CwztGUID = '" & oid & "' AND HierarchyCode = '006.002';"
                                strSql &= " IF NOT EXISTS (SELECT * FROM p_CwjkOperType WHERE IsDisable=0 AND CwztGUID='" & oid & "' AND HierarchyCode  LIKE '006.%' ) UPDATE p_CwjkOperType SET IsDisable = 1 WHERE CwztGUID='" & oid & "' AND HierarchyCode  = '006'; "
                            End If
                        End If

                        If (MyDB.ExecSQL(strSql) >= 0) Then

                            Return "<xml result=""true"" keyvalue=""" & oid & """></xml>"
                        End If
                    Else
                        Return "<xml result=""true"" keyvalue=""" & oid & """></xml>"
                    End If
                End If
			End If
		End If
		Return "<xml result=""false"" errormessage=""运行时异常，请与管理员联系！""></xml>"
	End Function
#End Region


#Region "账套保存公共函数"

	'检测模板文件是否存在
	Public Shared Function CheckTemplateIsExists(ByVal strTemplateName As String) As Boolean
		Dim strFilePath As String = HttpContext.Current.Server.MapPath("\PubProject\Cwjk\Files\" + strTemplateName)
		If File.Exists(strFilePath) Then
			Return True
		End If
		Return False
	End Function

	'保存账套其他表信息
	Public Shared Function SaveCwjkZtOther(ByVal oid As String, ByVal strBUGUID As String, ByVal strAppcation As String, ByVal strNewRemarks As String, ByVal strTemplateName As String) As String
		Dim newFileName As String = Guid.NewGuid.ToString()
		'如果解压文件成功
		If Decode("\TempFiles\", "\PubProject\Cwjk\Files\" + strTemplateName, newFileName + ".xml") Then
			Dim xml As XmlDocument = New XmlDocument
			Dim strRemarks As String
			xml.Load(HttpContext.Current.Server.MapPath("\TempFiles\" + newFileName + ".xml"))
			If Not xml Is Nothing Then
				Dim hashTable As Hashtable = New Hashtable()
				Dim hashFiltrate As Hashtable = New Hashtable()
				Dim strSQL As StringBuilder = New StringBuilder()
				strRemarks = DecodeChar(xml.DocumentElement.Attributes.GetNamedItem("Remarks").InnerText).Replace("'", "''")
				If strNewRemarks = "" Then
					strSQL.Append(" UPDATE p_CwjkCwzt SET 	Remarks = '" & strRemarks & "' WHERE CwztGUID = '" & oid & "';")
				End If
				SaveCwjkHsxm(xml, oid, strSQL, hashTable)	  
				SaveKjkm(xml, oid, strBUGUID, hashTable, strSQL, 0)
				SaveKjkm2OperData(xml, oid, strBUGUID, strSQL, hashFiltrate, New Hashtable(), 0)
				SaveCwjkKjkm2Hsxm(xml, oid, hashTable, strSQL)
				SaveCwjkOperType(xml, oid, strAppcation, hashTable, strSQL)
				SaveCwjkPzEntryRule(xml, oid, strAppcation, strSQL, 0)
				Dim strSQLs As String = strSQL.ToString()
				For Each de As DictionaryEntry In hashTable
					strSQLs = strSQLs.Replace(de.Key.ToString(), de.Value.ToString())
				Next
				If MyDB.ExecSQL(strSQLs) > 0 Then
					Return "<xml result=""true"" keyvalue=""" & oid & """></xml>"
				Else
					Return "<xml result=""false"" errormessage=""运行时异常，请与管理员联系！""></xml>"
				End If
			End If
		Else
			Return "<xml result=""false"" errormessage=""解压标准模板文件出错，不允许保存！""></xml>"
		End If
	End Function

	'保存辅助核算表信息
	Public Shared Function SaveCwjkHsxm(ByVal xml As XmlDocument, ByVal oid As String, ByRef strSQL As StringBuilder, ByRef hashTable As Hashtable) As String
		Dim xmlNodes As XmlNodeList
		Dim strOldHsGUID, strNewHsGUID As String
		xmlNodes = xml.SelectNodes("/Cwzt/Hsxms/Hsxm")
		If xmlNodes.Count > 0 Then
			For Each node As XmlNode In xmlNodes
				strOldHsGUID = node.Attributes("HsTypeGUID").Value.ToUpper()
				strNewHsGUID = Guid.NewGuid.ToString().ToUpper()
				hashTable.Add(strOldHsGUID, strNewHsGUID)
				strSQL.Append("INSERT INTO p_CwjkHsxm (	CwztGUID,OperObject,HsTypeGUID,	HsTypeName,	HsTypeNameU8 )VALUES( ")
				strSQL.Append(" '" & oid & "','")
				strSQL.Append(DecodeChar(node.Attributes("OperObject").Value).Replace("'", "''") & "','")
				strSQL.Append(strNewHsGUID & "','")
				strSQL.Append(DecodeChar(node.Attributes("HsTypeName").Value).Replace("'", "''") & "', '")
				strSQL.Append(DecodeChar(node.Attributes("HsTypeNameU8").Value).Replace("'", "''") & "');")
			Next
		End If
		Return ""
	End Function

	'保存辅助核算明细表信息	   
	Public Shared Function SaveCwjkHsxmMap(ByVal xml As XmlDocument, ByVal oid As String, ByVal strBUGUID As String, ByRef strSQL As StringBuilder, ByRef hashTableFiltrate As Hashtable, ByRef hashTableHsFiltrate As Hashtable, ByRef isImp As String) As String
		Dim xmlNodes As XmlNodeList
	 	Dim strObjectGUID, strKjkmGUID, strHsTypeGUID As String
		xmlNodes = xml.SelectNodes("/Cwzt/Hsxms/Hsxm/HsxmMaps/HsxmMap")
		If xmlNodes.Count > 0 Then
			For Each node As XmlNode In xmlNodes
				strObjectGUID = node.Attributes("ObjectGUID").Value.ToUpper()
				If isImp = 0 Then
					strSQL.Append("INSERT INTO p_CwjkHsxmMap(BUGUID, CwztGUID, HsxmMapGUID,	HsTypeGUID,	HsxmCode,HsxmName,ObjectGUID,ObjectName)VALUES( ")
					strSQL.Append(" '" & strBUGUID & "','")
					strSQL.Append(oid & "',")
					strSQL.Append("newid(),'")
					strSQL.Append(node.Attributes("HsTypeGUID").Value.ToUpper() & "', '")
					strSQL.Append(DecodeChar(node.Attributes("HsxmCode").Value).Replace("'", "''") & "','")
					strSQL.Append(DecodeChar(node.Attributes("HsxmName").Value).Replace("'", "''") & "',")
					strSQL.Append(IIf(strObjectGUID = "", "NULL", "'" & strObjectGUID & "'") & ",'")
					strSQL.Append(DecodeChar(node.Attributes("ObjectName").Value).Replace("'", "''") & "');")
				Else
					strHsTypeGUID = node.Attributes("HsTypeGUID").Value.ToUpper()
					'如果不是售楼项目、楼栋、房间、预约单，成本项目，科目、合同、产品等核算项，直接添加
					If Not hashTableHsFiltrate.ContainsKey(strHsTypeGUID) Then
						strSQL.Append("INSERT INTO p_CwjkHsxmMap(BUGUID, CwztGUID, HsxmMapGUID,	HsTypeGUID,	HsxmCode,HsxmName,ObjectGUID,ObjectName)VALUES( ")
						strSQL.Append(" '" & strBUGUID & "','")
						strSQL.Append(oid & "',")
						strSQL.Append("newid(),'")
						strSQL.Append(strHsTypeGUID & "', '")
						strSQL.Append(DecodeChar(node.Attributes("HsxmCode").Value).Replace("'", "''") & "','")
						strSQL.Append(DecodeChar(node.Attributes("HsxmName").Value).Replace("'", "''") & "',")
						strSQL.Append(IIf(strObjectGUID = "", "NULL", "'" & strObjectGUID & "'") & ",'")
						strSQL.Append(DecodeChar(node.Attributes("ObjectName").Value).Replace("'", "''") & "');")
						'如果是售楼项目、楼栋、房间、预约单，成本项目，科目、合同、产品等核算项，在账套项目中则添加
					ElseIf hashTableFiltrate.ContainsKey(strObjectGUID) Then
						strSQL.Append("INSERT INTO p_CwjkHsxmMap(BUGUID, CwztGUID, HsxmMapGUID,	HsTypeGUID,	HsxmCode,HsxmName,ObjectGUID,ObjectName)VALUES( ")
						strSQL.Append(" '" & strBUGUID & "','")
						strSQL.Append(oid & "',")
						strSQL.Append("newid(),'")
						strSQL.Append(strHsTypeGUID & "', '")
						strSQL.Append(DecodeChar(node.Attributes("HsxmCode").Value).Replace("'", "''") & "','")
						strSQL.Append(DecodeChar(node.Attributes("HsxmName").Value).Replace("'", "''") & "',")
						strSQL.Append(IIf(strObjectGUID = "", "NULL", "'" & strObjectGUID & "'") & ",'")
						strSQL.Append(DecodeChar(node.Attributes("ObjectName").Value).Replace("'", "''") & "');")
					End If
				End If
			Next
		End If
		Return ""
	End Function
	'保存会计科目表信息
	Public Shared Function SaveKjkm(ByVal xml As XmlDocument, ByVal oid As String, ByVal strBUGUID As String, ByRef hashTable As Hashtable, ByRef strSQL As StringBuilder, ByVal strLevel As String) As String
		Dim xmlNodes As XmlNodeList
		Dim strOldKjKmGUID, strNewKjkmGUID As String
		If strLevel = 1 Then
			xmlNodes = xml.SelectNodes("/Cwzt/Kjkms/Kjkm[@KjkmLevel='1']")
		Else
			xmlNodes = xml.SelectNodes("/Cwzt/Kjkms/Kjkm")
		End If

		If xmlNodes.Count > 0 Then
			For Each node As XmlNode In xmlNodes
				strOldKjKmGUID = node.Attributes("KjkmGUID").Value.ToUpper()
				strNewKjkmGUID = Guid.NewGuid.ToString().ToUpper()
				hashTable.Add(strOldKjKmGUID, strNewKjkmGUID)
				strSQL.Append("INSERT INTO p_CwjkKjkm (	KjkmGUID,BUGUID,CwztGUID,KjkmCode,KjkmName,KjkmLevel,ObjectDataText,ObjectType,	HsxmObjectText,	IsEnd,	IsSys,	ShortCode,	ParentCode,	KjkmOrderCode,	KjkmParentName,	KjkmFullName,ObjectData) VALUES( ")
				strSQL.Append("'" & strNewKjkmGUID & "','")
				strSQL.Append(strBUGUID & "','")
				strSQL.Append(oid & "','")
				strSQL.Append(DecodeChar(node.Attributes("KjkmCode").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("KjkmName").Value).Replace("'", "''") & "','")
				strSQL.Append(node.Attributes("KjkmLevel").Value & "','")
				strSQL.Append(DecodeChar(node.Attributes("ObjectDataText").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("ObjectType").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("HsxmObjectText").Value).Replace("'", "''") & "','")
				strSQL.Append(IIf(strLevel = "1", "1", node.Attributes("IsEnd").Value) & "','")
				strSQL.Append(node.Attributes("IsSys").Value & "','")
				strSQL.Append(DecodeChar(node.Attributes("ShortCode").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("ParentCode").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("KjkmOrderCode").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("KjkmParentName").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("KjkmFullName").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("ObjectData").Value).Replace("'", "''") & "');")
			Next
		End If
		Return ""
	End Function
	'保存会计科目2业务数据表信息
	Public Shared Function SaveKjkm2OperData(ByVal xml As XmlDocument, ByVal oid As String, ByVal strBUGUID As String, ByRef strSQL As StringBuilder, ByVal hashTableFiltrate As Hashtable, ByVal hashTableHsFiltrate As Hashtable, ByVal isImp As String) As String
		Dim xmlNodes As XmlNodeList
		
		xmlNodes = xml.SelectNodes("/Cwzt/Kjkms/Kjkm/Kjkm2OperDatas/Kjkm2OperData")
		If xmlNodes.Count > 0 Then
			Dim strKjkmGUID As String
			For Each node As XmlNode In xmlNodes
				If isImp = 0 Then
					strSQL.Append("INSERT INTO p_CwjkKjkm2OperData(Kjkm2OperDataGUID,CwztGUID,BUGUID,KjkmGUID,OperObject,ObjectGUID,ObjectName,ObjectKjkmGUID)VALUES(")
					strSQL.Append("newid(),'")
					strSQL.Append(oid & "','")
					strSQL.Append(strBUGUID & "','")
					strSQL.Append(node.Attributes("KjkmGUID").Value.ToUpper() & "','")
					strSQL.Append(DecodeChar(node.Attributes("OperObject").Value).Replace("'", "''") & "','")
					strSQL.Append(node.Attributes("ObjectGUID").Value & "','")
					strSQL.Append(DecodeChar(node.Attributes("ObjectName").Value).Replace("'", "''") & "','")
					strSQL.Append(node.Attributes("ObjectKjkmGUID").Value.ToUpper() & "');")
				Else
					strKjkmGUID = node.Attributes("KjkmGUID").Value.ToUpper()
					'如果不是售楼项目、楼栋，成本项目，科目、合同、产品等核算项，直接添加
					If Not hashTableHsFiltrate.ContainsKey(strKjkmGUID) Then
						strSQL.Append("INSERT INTO p_CwjkKjkm2OperData(Kjkm2OperDataGUID,CwztGUID,BUGUID,KjkmGUID,OperObject,ObjectGUID,ObjectName,ObjectKjkmGUID)VALUES(")
						strSQL.Append("newid(),'")
						strSQL.Append(oid & "','")
						strSQL.Append(strBUGUID & "','")
						strSQL.Append(strKjkmGUID & "','")
						strSQL.Append(DecodeChar(node.Attributes("OperObject").Value).Replace("'", "''") & "','")
						strSQL.Append(node.Attributes("ObjectGUID").Value & "','")
						strSQL.Append(DecodeChar(node.Attributes("ObjectName").Value).Replace("'", "''") & "','")
						strSQL.Append(node.Attributes("ObjectKjkmGUID").Value.ToUpper() & "');")
						'如果是售楼项目、楼栋，成本项目，科目、合同、产品等核算项，在账套项目中则添加
					ElseIf hashTableFiltrate.ContainsKey(node.Attributes("ObjectGUID").Value.ToUpper()) Then
						strSQL.Append("INSERT INTO p_CwjkKjkm2OperData(Kjkm2OperDataGUID,CwztGUID,BUGUID,KjkmGUID,OperObject,ObjectGUID,ObjectName,ObjectKjkmGUID)VALUES(")
						strSQL.Append("newid(),'")
						strSQL.Append(oid & "','")
						strSQL.Append(strBUGUID & "','")
						strSQL.Append(strKjkmGUID & "','")
						strSQL.Append(DecodeChar(node.Attributes("OperObject").Value).Replace("'", "''") & "','")
						strSQL.Append(node.Attributes("ObjectGUID").Value & "','")
						strSQL.Append(DecodeChar(node.Attributes("ObjectName").Value).Replace("'", "''") & "','")
						strSQL.Append(node.Attributes("ObjectKjkmGUID").Value.ToUpper() & "');")
					End If
				End If

			Next
		End If
		Return ""
	End Function

	'保存会计科目2辅助核算关系表
	Public Shared Function SaveCwjkKjkm2Hsxm(ByVal xml As XmlDocument, ByVal oid As String, ByRef hashTable As Hashtable, ByRef strSQL As StringBuilder) As String
		Dim xmlNodes As XmlNodeList
		xmlNodes = xml.SelectNodes("/Cwzt/Kjkms/Kjkm/Kjkm2Hsxms/Kjkm2Hsxm")
		Dim strHsTypeGUID, strKjkmGUID As String
		If xmlNodes.Count > 0 Then
			For Each node As XmlNode In xmlNodes
				strHsTypeGUID = node.Attributes("HsTypeGUID").Value
				strKjkmGUID = node.Attributes("KjkmGUID").Value

				strSQL.Append("INSERT INTO p_CwjkKjkm2Hsxm(	Kjkm2HsxmGUID,CwztGUID,KjkmGUID,HsTypeGUID,HsNum)VALUES(")
				strSQL.Append("newid(),'")
				strSQL.Append(oid & "',")
				strSQL.Append(IIf(strKjkmGUID = "", "NULL", "'" & strKjkmGUID.ToUpper() & "'") & ",")
				strSQL.Append(IIf(strHsTypeGUID = "", "NULL", "'" & strHsTypeGUID.ToUpper() & "'") & ",'")
				strSQL.Append(node.Attributes("HsNum").Value & "');")
			Next
		End If
		Return ""
	End Function


	'保存业务类型数据
	Public Shared Function SaveCwjkOperType(ByVal xml As XmlDocument, ByVal oid As String, ByVal strApplication As String, ByRef hashTable As Hashtable, ByRef strSQL As StringBuilder) As String
		Dim xmlNodes As XmlNodeList
		Dim strOldOperTypeGUID, strNewOperTypeGUID As String
		xmlNodes = xml.SelectNodes("/Cwzt/OperTypes/OperType")
		If xmlNodes.Count > 0 Then
			For Each node As XmlNode In xmlNodes
				strOldOperTypeGUID = node.Attributes("OperTypeGUID").Value.ToUpper()
				strNewOperTypeGUID = Guid.NewGuid.ToString().ToUpper()
				hashTable.Add(strOldOperTypeGUID, strNewOperTypeGUID)
				strSQL.Append("INSERT INTO p_CwjkOperType(OperTypeGUID,CwztGUID,Application,OperType,OperTypeLevel,DataSource,DataFilter,SecondFilter,ZYFiled,")
				strSQL.Append("AmountFiled,SpecialRule,OperTypeCode,HierarchyCode,UsedScope,IsShowUsedScope,IsUsedScopeRepeat,IsSys,IsDisable,IsAddible)VALUES(")
				strSQL.Append("'" & strNewOperTypeGUID & "','")
				strSQL.Append(oid & "','")
				strSQL.Append(strApplication & "','")
				strSQL.Append(node.Attributes("OperType").Value & "','")
				strSQL.Append(node.Attributes("OperTypeLevel").Value & "','")
				strSQL.Append(DecodeChar(node.Attributes("DataSource").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("DataFilter").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("SecondFilter").Value).Replace("'", "''") & "','")
				strSQL.Append(node.Attributes("ZyFiled").Value & "','")
				strSQL.Append(node.Attributes("AmountFiled").Value & "','")
				strSQL.Append(node.Attributes("SpecialRule").Value & "','")
				strSQL.Append(node.Attributes("OperTypeCode").Value & "','")
				strSQL.Append(node.Attributes("HierarchyCode").Value & "','")
				strSQL.Append(node.Attributes("UsedScope").Value & "','")
				strSQL.Append(node.Attributes("IsShowUsedScope").Value & "','")
				strSQL.Append(node.Attributes("IsUsedScopeRepeat").Value & "','")
				strSQL.Append(node.Attributes("IsSys").Value & "','")
				strSQL.Append(node.Attributes("IsDisable").Value & "','")
				strSQL.Append(node.Attributes("IsAddible").Value & "');")
			Next
		End If
		Return ""
	End Function
	'保存凭证分录规则
	Public Shared Function SaveCwjkPzEntryRule(ByVal xml As XmlDocument, ByVal oid As String, ByVal strApplication As String, ByRef strSQL As StringBuilder, ByVal strLevel As String) As String
		Dim xmlNodes, xmlFirstKm As XmlNodeList
		Dim hashFirstKm As Hashtable = New Hashtable()	 '存放科目CODE和GUID    
		Dim hashFirstGUID As Hashtable = New Hashtable() '存放科目与一级科目GUID对照
		Dim strKjkmCode As String
		Dim strKjkmCodes As String()
		'如果只导入级科目
		If strLevel = 1 Then
			xmlFirstKm = xml.SelectNodes("/Cwzt/Kjkms/Kjkm")
			For Each node As XmlNode In xmlFirstKm
				If Not hashFirstKm.ContainsKey(node.Attributes("KjkmOrderCode").Value) Then
					hashFirstKm.Add(node.Attributes("KjkmOrderCode").Value, node.Attributes("KjkmGUID").Value)
				End If
			Next

			For Each de As DictionaryEntry In hashFirstKm
				strKjkmCode = de.Key
				strKjkmCodes = strKjkmCode.Split(".")
				If strKjkmCodes.Length = 1 Then
					hashFirstGUID.Add(de.Value.ToString().ToUpper(), de.Value.ToString().ToUpper())
				Else
					If hashFirstKm.ContainsKey(strKjkmCodes(0)) Then
						hashFirstGUID.Add(de.Value.ToString().ToUpper(), hashFirstKm.Item(strKjkmCodes(0)).ToString().ToUpper())
					End If
				End If
			Next
		End If

		Dim strKjkmGUID As String
		xmlNodes = xml.SelectNodes("/Cwzt/OperTypes/OperType/EntryRules/EntryRule")
		If xmlNodes.Count > 0 Then
			For Each node As XmlNode In xmlNodes
				strKjkmGUID = node.Attributes("KjkmGUID").Value.ToUpper()
				If strLevel = 1 AndAlso strKjkmGUID <> "" Then	 
					If hashFirstGUID.ContainsKey(strKjkmGUID) Then
						strKjkmGUID = hashFirstGUID.Item(strKjkmGUID).ToString().ToUpper()
					End If
				End If
				strSQL.Append("INSERT INTO p_CwjkPzEntryRule(CwztGUID,OperTypeGUID,PzEntryRuleGUID,Zy,ZySql,AmountFiled,YDDirection,KmXmlFilterRule,KmSqlFilterRule,")
				strSQL.Append(" IsInversion,EntryNum,ZyText,KmTextFilterRule,AmountFiledCN,KjkmGUID,Pzz,PzName,GroupName,IsDisable,PzOrderCode,IsAddEntry)VALUES(")
				strSQL.Append("'" & oid & "','")
				strSQL.Append(node.Attributes("OperTypeGUID").Value.ToUpper() & "',newid(),'")
				strSQL.Append(DecodeChar(node.Attributes("Zy").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("ZySql").Value).Replace("'", "''") & "','")
				strSQL.Append(node.Attributes("AmountFiled").Value & "','")
				strSQL.Append(node.Attributes("YDDirection").Value & "','")
				strSQL.Append(DecodeChar(node.Attributes("KmXmlFilterRule").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("KmSqlFilterRule").Value).Replace("'", "''") & "','")
				strSQL.Append(node.Attributes("IsInversion").Value & "','")
				strSQL.Append(node.Attributes("EntryNum").Value & "','")
				strSQL.Append(DecodeChar(node.Attributes("ZyText").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("KmTextFilterRule").Value).Replace("'", "''") & "','")
				strSQL.Append(node.Attributes("AmountFiledCN").Value & "',")
				strSQL.Append(IIf(strKjkmGUID = "", "NULL", "'" & strKjkmGUID.ToUpper() & "'") & ",'")
				strSQL.Append(DecodeChar(node.Attributes("Pzz").Value).Replace("'", "''") & "','")
				strSQL.Append(DecodeChar(node.Attributes("PzName").Value).Replace("'", "''") & "','")
				strSQL.Append(node.Attributes("GroupName").Value & "','")
				strSQL.Append(node.Attributes("IsDisable").Value & "','")
				strSQL.Append(node.Attributes("PzOrderCode").Value & "','")
				strSQL.Append(node.Attributes("IsAddEntry").Value & "');")
			Next
		End If
		Return ""
	End Function
#End Region

#Region "账套导出"
	
	'导出账套
	Public Shared Function ExportZT(ByVal DataXml As String, ByVal UserDataXml As String) As String
		Dim xmlDom As New XmlDocument
		Dim dtTemp1 As DataTable
		Dim oid, strRemarks, strSQL, strZtName As String
		Dim strXml As StringBuilder = New StringBuilder("<?xml version='1.0' encoding='gb2312' ?>")
		xmlDom.LoadXml(DataXml)
		oid = xmlDom.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText
		strRemarks = xmlDom.SelectSingleNode("/ExportCwzt/Remarks").InnerText
		strZtName = xmlDom.SelectSingleNode("/ExportCwzt/CwztName").InnerText
		'去掉账套名中的特殊字符
		strZtName = strZtName.Replace("\", "").Replace("/", "").Replace(":", "").Replace("*", "").Replace("|", "").Replace("?", "").Replace(">", "").Replace("<", "").Replace("""", "")

		'账套基本信息
		strSQL = "SELECT CwztGUID,Application,JzMode,case  when IsPOS=1 then 1 else 0 end as IsPos ,'" & strRemarks & "' AS  Remarks FROM p_CwjkCwzt WHERE CwztGUID = '" & oid & "'"
		dtTemp1 = MyDB.GetDataTable(strSQL)
		Try
			If dtTemp1.Rows.Count > 0 Then
				strXml.Append("<Cwzt ")
				strXml.Append(GetFieldsValue(dtTemp1, dtTemp1.Rows(0), dtTemp1.Columns.Count))
				strXml.Append(" >")
				ExportHsxm(oid, strXml)
				ExportCwjkKjkm(oid, strXml)
				ExportCwjkOperType(oid, strXml)
				strXml.Append("</Cwzt>")
				Dim strDirOid As String = Guid.NewGuid.ToString()
				If ExportCwZtSet(strDirOid, strXml.ToString(), strZtName) Then 
					Return "<xml result=""true"" keyvalue=""" & oid & """ fileName=""" & strZtName & ".zip" & """ fileUrl=""/TempFiles/" & strDirOid & "/" & strZtName & ".zip" & """></xml>"
				End If
			End If
		Catch ex As Exception
			Return "<xml result=""false"" errormessage=""生成账套文件失败，请与管理员联系！""></xml>"
		End Try
	End Function

    '导出账套设置
	Public Shared Function ExportCwZtSet(ByVal strDirOid As String, ByVal strXml As String, ByVal strFileName As String) As Boolean
		'0、变量定义和初始化
		Dim strPath As String = ""
		Dim strXmlFileName As String = "账套设置数据文件.xml"
		Dim strZipFileName As String = strFileName & ".zip"
		Dim strXmlFileFullName, strZipFileFullName As String

        '1、在根目录（Map\TempFiles）下创建存放xml文件的临时目录，以账套GUID命名
		strPath = HttpContext.Current.Server.MapPath("\TempFiles\" & strDirOid)
		If Not IO.Directory.Exists(strPath) Then
			IO.Directory.CreateDirectory(strPath)
		End If

        '2、根据这个账套的数据，创建XML文件，放到所创建的目录下

		'获取文件全名
		strXmlFileFullName = strPath & "\" & strXmlFileName
		strZipFileFullName = strPath & "\" & strZipFileName



		'删除已经存在的xml文件
		If File.Exists(strXmlFileFullName) Then
			File.Delete(strXmlFileFullName)
		End If

		Dim doc As XmlDocument = New XmlDataDocument
		doc.LoadXml(strXml)
		doc.Save(strXmlFileFullName)

		'3、把这个目录下的文件创建为zip文件
		'删除已经存在的zip文件
		If File.Exists(strZipFileFullName) Then
			File.Delete(strZipFileFullName)
		End If

		'生成zip文件
		Return CreateZip(strPath, strZipFileFullName)

		'以下两步在download页面中完成
		'4、下载zip文件
		'5、删除所创建的目录和目录下的文件	 

	End Function

	'导出辅助核算数据
	Public Shared Function ExportHsxm(ByVal oid As String, ByRef strXml As StringBuilder) As String
		Dim strSQL As String = "SELECT HsTypeGUID,CwztGUID,HsTypeName,OperObject,HsTypeNameU8 FROM p_CwjkHsxm  WHERE CwztGUID='" & oid & "'"
		Dim dtTemp1, dtTemp2 As DataTable
		Dim strHsTypeGUID As String
		dtTemp1 = MyDB.GetDataTable(strSQL)
		strSQL = "SELECT HsTypeGUID,BUGUID, CwztGUID, HsxmMapGUID,	HsxmCode,HsxmName,ObjectGUID,ObjectName FROM p_CwjkHsxmMap WHERE CwztGUID='" & oid & "'"
		dtTemp2 = MyDB.GetDataTable(strSQL)
		strXml.Append("<Hsxms>")
		For Each row As DataRow In dtTemp1.Rows
			strHsTypeGUID = row.Item("HsTypeGUID").ToString()
			strXml.Append("<Hsxm ")
			strXml.Append(GetFieldsValue(dtTemp1, row, dtTemp1.Columns.Count))
			strXml.Append(">")
			ExportHsxmMap(dtTemp2, dtTemp2.Select("HsTypeGUID='" & strHsTypeGUID & "'"), strXml)
			strXml.Append("</Hsxm>")
		Next
		strXml.Append("</Hsxms>")
		Return ""
	End Function

	'导出辅助核算明细数据
	Public Shared Function ExportHsxmMap(ByVal dtTemp As DataTable, ByVal dtRows() As DataRow, ByRef strXml As StringBuilder) As String
		strXml.Append("<HsxmMaps>")
		For Each row As DataRow In dtRows
			strXml.Append("<HsxmMap ")
			strXml.Append(GetFieldsValue(dtTemp, row, dtTemp.Columns.Count))
			strXml.Append(">")
			strXml.Append("</HsxmMap>")
		Next
		strXml.Append("</HsxmMaps>")
		Return ""
	End Function

	'导出会计科目数据
	Public Shared Function ExportCwjkKjkm(ByVal oid As String, ByRef strXml As StringBuilder) As String
		Dim strSQL As String = "SELECT KjkmGUID,BUGUID, CwztGUID, KjkmCode, KjkmName, KjkmLevel,ShortCode, ParentCode, KjkmOrderCode, " & _
		  " ObjectDataText,ObjectType, HsxmObjectText, case when IsEnd= 0 then 0 else 1 end as  IsEnd,case when IsSys=0 then 0 else 1 end IsSys, ObjectData, KjkmParentName, KjkmFullName " & _
		  " FROM p_CwjkKjkm   WHERE CwztGUID='" & oid & "'"
		Dim dtTemp1, dtTemp2, dtTemp3 As DataTable
		Dim strKjkmGUID As String
		dtTemp1 = MyDB.GetDataTable(strSQL)
		strSQL = "  SELECT Kjkm2OperDataGUID, CwztGUID, BUGUID, KjkmGUID, OperObject, ObjectGUID,ObjectName, ObjectKjkmGUID FROM p_CwjkKjkm2OperData  WHERE CwztGUID='" & oid & "'"
		dtTemp2 = MyDB.GetDataTable(strSQL)
		strSQL = "  SELECT CwztGUID, KjkmGUID, HsTypeGUID, HsNum FROM p_CwjkKjkm2Hsxm  WHERE CwztGUID='" & oid & "'"
		dtTemp3 = MyDB.GetDataTable(strSQL)
		strXml.Append("<Kjkms>")
		For Each row As DataRow In dtTemp1.Rows
			strKjkmGUID = row.Item("KjkmGUID").ToString()
			strXml.Append("<Kjkm ")
			strXml.Append(GetFieldsValue(dtTemp1, row, dtTemp1.Columns.Count))
			strXml.Append(">")
			ExportCwjkKjkmOperData(dtTemp2, dtTemp2.Select("KjkmGUID='" & strKjkmGUID & "'"), strXml)
			ExportCwjkKjkmHsxm(dtTemp3, dtTemp3.Select("KjkmGUID='" & strKjkmGUID & "'"), strXml)
			strXml.Append("</Kjkm>")
		Next
		strXml.Append("</Kjkms>")
		Return ""
	End Function

	'导出会计科目2业务数据表、会计科目2辅助核算关系表数据
	Public Shared Function ExportCwjkKjkmOperData(ByVal dtTemp As DataTable, ByVal dtRows() As DataRow, ByRef strXml As StringBuilder) As String
		strXml.Append("<Kjkm2OperDatas>")
		For Each row As DataRow In dtRows
			strXml.Append("<Kjkm2OperData ")
			strXml.Append(GetFieldsValue(dtTemp, row, dtTemp.Columns.Count))
			strXml.Append(">")
			strXml.Append("</Kjkm2OperData>")
		Next
		strXml.Append("</Kjkm2OperDatas>")
		Return ""
	End Function

	'导出会计科目2辅助核算关系表数据
	Public Shared Function ExportCwjkKjkmHsxm(ByVal dtTemp As DataTable, ByVal dtRows() As DataRow, ByRef strXml As StringBuilder) As String
		strXml.Append("<Kjkm2Hsxms>")
		For Each row As DataRow In dtRows
			strXml.Append("<Kjkm2Hsxm ")
			strXml.Append(GetFieldsValue(dtTemp, row, dtTemp.Columns.Count))
			strXml.Append(">")
			strXml.Append("</Kjkm2Hsxm>")
		Next
		strXml.Append("</Kjkm2Hsxms>")
		Return ""
	End Function


	'导出业务类型数据
	Public Shared Function ExportCwjkOperType(ByVal oid As String, ByRef strXml As StringBuilder) As String
		Dim strSQL As String = "SELECT OperTypeGUID, CwztGUID, Application, OperType, OperTypeLevel, DataSource,DataFilter,SecondFilter," & _
		  " ZyFiled, AmountFiled, SpecialRule, OperTypeCode, HierarchyCode, UsedScope,case when IsShowUsedScope = 0 then 0 else 1 end as IsShowUsedScope, " & _
		  " case when IsUsedScopeRepeat = 0 then 0 else 1 end IsUsedScopeRepeat,case when IsSys = 0 then 0 else 1 end as IsSys," & _
		  " case when IsDisable = 0 then 0 else 1 end IsDisable,case when IsAddible = 0 then 0 else 1 end IsAddible FROM p_CwjkOperType  WHERE CwztGUID='" & oid & "'"
		Dim dtTemp1, dtTemp2 As DataTable
		Dim strOperTypeGUID As String
		dtTemp1 = MyDB.GetDataTable(strSQL)
		strSQL = "SELECT CwztGUID, OperTypeGUID, PzEntryRuleGUID, Zy, ZySql, AmountFiled,YDDirection, KmXmlFilterRule, KmSqlFilterRule, " & _
		   " IsInversion, EntryNum,ZyText, KmTextFilterRule, AmountFiledCN, KjkmGUID, Pzz, PzName, GroupName, case when IsDisable =0 then 0 else 1 end IsDisable, PzOrderCode,IsAddEntry " & _
		   " FROM p_CwjkPzEntryRule   WHERE CwztGUID='" & oid & "'"
		dtTemp2 = MyDB.GetDataTable(strSQL)
		strXml.Append("<OperTypes>")
		For Each row As DataRow In dtTemp1.Rows
			strOperTypeGUID = row.Item("OperTypeGUID").ToString()
			strXml.Append("<OperType ")
			strXml.Append(GetFieldsValue(dtTemp1, row, dtTemp1.Columns.Count))
			strXml.Append(">")
			ExportCwjkPzEntryRule(dtTemp2, dtTemp2.Select("OperTypeGUID='" & strOperTypeGUID & "'"), strXml)
			strXml.Append("</OperType>")
		Next
		strXml.Append("</OperTypes>")
		Return ""
	End Function

	'导出凭证分录规则数据
	Public Shared Function ExportCwjkPzEntryRule(ByVal dtTemp As DataTable, ByVal dtRows() As DataRow, ByRef strXml As StringBuilder) As String
		strXml.Append("<EntryRules>")
		For Each row As DataRow In dtRows
			strXml.Append("<EntryRule ")
			strXml.Append(GetFieldsValue(dtTemp, row, dtTemp.Columns.Count))
			strXml.Append(">")
			strXml.Append("</EntryRule>")
		Next
		strXml.Append("</EntryRules>")
		Return ""
	End Function

	'功能：生成某一行的字段值串 
	'格式：fieldName='fieldValue'
	Public Shared Function GetFieldsValue(ByVal dtTmp As DataTable, ByVal drTmp As DataRow, ByVal iColumns As Integer) As String
		If drTmp Is Nothing Then Return ""

		Dim index As Integer
		Dim strRtn As String = ""
		For index = 0 To iColumns - 1
			strRtn &= dtTmp.Columns(index).ColumnName & "='" & Mysoft.PubProject.Business.PublicMNG.ReplaceXmlChar(drTmp.Item(index).ToString()) & "' "
		Next
		Return strRtn
	End Function
#End Region


#Region "导入账套"
	'导入账套
	Public Shared Function ImportZT(ByVal DataXml As String, ByVal UserDataXml As String) As String
		Dim xmlDom, xml As New XmlDocument
		Dim dtTemp1 As DataTable
		Dim strSQLs, oid, strRemarks, strFileName, strApplication, strZtImpWay, strImpCheck, strNewFileName, strBUGUID As String
		Dim strSQL As StringBuilder = New StringBuilder()
		Try
			xmlDom.LoadXml(DataXml)
			oid = xmlDom.DocumentElement.Attributes.GetNamedItem("keyvalue").InnerText
			strRemarks = xmlDom.SelectSingleNode("/ImportCwzt/Remarks").InnerText
			strFileName = xmlDom.SelectSingleNode("/ImportCwzt/fileName").InnerText
			strApplication = xmlDom.SelectSingleNode("/ImportCwzt/Application").InnerText
			strZtImpWay = xmlDom.SelectSingleNode("/ImportCwzt/ZtImpWay").InnerText
			strImpCheck = xmlDom.SelectSingleNode("/ImportCwzt/ImpCheck").InnerText	    
			strBUGUID = HttpContext.Current.Session("BUGUID")
			strImpCheck = IIf(strImpCheck = "0", "1", "0")
			Dim hashTable As Hashtable = New Hashtable()
			If strZtImpWay = 1 Then
				strNewFileName = Guid.NewGuid.ToString()
				If Decode("\TempFiles\", "\PubProject\Cwjk\Files\" + strFileName, strNewFileName + ".xml") Then
					strFileName = strNewFileName + ".xml"
				Else
					Return "<xml result=""false"" errormessage=""解压标准模板文件出错，导入失败！""></xml>"
				End If
			End If
			strFileName = HttpContext.Current.Server.MapPath("\TempFiles\" + strFileName)
			If File.Exists(strFileName) Then
				xml.Load(strFileName)
				DeleteInfo(oid, strSQL)
				Dim strIsPos, strJzMode As String
				strIsPos = xml.DocumentElement.Attributes.GetNamedItem("IsPos").InnerText
				strJzMode = xml.DocumentElement.Attributes.GetNamedItem("JzMode").InnerText
				strSQL.Append("UPDATE p_CwjkCwzt SET isPos = '" & strIsPos & "',JzMode = '" & strJzMode & "',Remarks = '" & strRemarks.Replace("'", "''") & "' WHERE CwztGUID = '" & oid & "';")
				SaveCwjkHsxm(xml, oid, strSQL, hashTable)
				SaveKjkm(xml, oid, strBUGUID, hashTable, strSQL, strImpCheck)
				SaveCwjkKjkm2Hsxm(xml, oid, hashTable, strSQL)
				SaveCwjkOperType(xml, oid, strApplication, hashTable, strSQL)
				SaveCwjkPzEntryRule(xml, oid, strApplication, strSQL, strImpCheck)
				'导入公司级科目时
				If strImpCheck = 0 Then
					ImportHsxmMap(xml, oid, strBUGUID, strApplication, strSQL)
                    ImportKjkm2OperData(xml, oid, strBUGUID, strApplication, strSQL)
                    strSQL.Append("UPDATE p_CwjkKjkm SET objecttype=0 WHERE CwztGUID='" & oid & "' AND isend=1 AND objecttype=2 AND not exists(select * from p_CwjkKjkm2OperData where kjkmguid=p_CwjkKjkm.kjkmguid);")
				End If

				strSQLs = strSQL.ToString()
				For Each de As DictionaryEntry In hashTable
					strSQLs = strSQLs.Replace(de.Key.ToString(), de.Value.ToString())
				Next
				'huzw 测试用 
				''My.Computer.FileSystem.WriteAllText("D:/work/ERP254new/明源整体解决方案/map/TempFiles/test.txt", strSQLs, True)
				If MyDB.ExecSQL(strSQLs) > 0 Then
					Return "<xml result=""true"" keyvalue=""" & oid & """></xml>"
				Else	    
					Return "<xml result=""false"" errormessage=""运行时异常，请与管理员联系！""></xml>"
				End If
			Else
				Return "<xml result=""false"" errormessage=""模板文件不存在，导入失败！""></xml>"
			End If
		Catch ex As Exception
			Return "<xml result=""false"" errormessage=""导入账套失败，请与管理员联系！""></xml>"
		End Try
	End Function

	'引入辅助核算明细
	Public Shared Function ImportHsxmMap(ByVal xml As XmlDocument, ByVal oid As String, ByVal strBUGUID As String, ByVal strApplication As String, ByVal strSQL As StringBuilder)
		Dim dtTemp As DataTable
		Dim hashTableFiltrate As Hashtable = New Hashtable
		Dim hashTableHsFiltrate As Hashtable = New Hashtable
		Dim nodes As XmlNodeList
		'售楼系统过滤项目、订单、楼栋、房间
		If strApplication = "0101" Then
			dtTemp = MyDB.GetDataTable("SELECT a.BldGUID AS ProjGUID FROM p_Building a " & _
			   " INNER JOIN p_CwjkProject b ON a.ProjGUID = b.ProjGUID AND b.CwztGUID = '" & oid & "' " & _
			   " UNION ALL " & _
			   " SELECT c.RoomGUID AS ProjGUID FROM p_room c " & _
			   " INNER JOIN p_CwjkProject d ON d.ProjGUID = c.ProjGUID AND d.CwztGUID = '" & oid & "'" & _
			   " UNION ALL " & _
			   " SELECT e.BookingGUID AS ProjGUID FROM s_Booking e " & _
			   " INNER JOIN p_CwjkProject f ON e.ProjGUID = f.ProjGUID AND f.CwztGUID = '" & oid & "' " & _
			   " UNION ALL " & _
			   " SELECT g.ProjGUID  AS ProjGUID FROM p_CwjkProject g WHERE CwztGUID = '" & oid & "'")
			nodes = xml.SelectNodes("/Cwzt/Hsxms/Hsxm[@OperObject='房间信息' or @OperObject='楼栋名称' or @OperObject='项目名称' or @OperObject='预约单']")
		
		Else '成本过滤项目、合同、科目、产品
			dtTemp = MyDB.GetDataTable(" SELECT contractguid AS projguid FROM  cb_Contract WHERE projtype in ('单项目','多项目') and contractguid in (  " & _
			   " select contractguid from  cb_ContractProj  a INNER JOIN p_CwjkProject  b ON a.ProjGUID = b.ProjGUID AND b.CwztGUID = '" & oid & "' ) " & _
			   " UNION ALL " & _
			   " SELECT  contractGUID AS projguid  from cb_contract c " & _
			   " INNER JOIN p_CwjkProject d ON d.ProjGUID = c.BUGUID AND d.CwztGUID = '" & oid & "' " & _
			   " where projtype in('公司库存','无项目','零星采购合同') " & _
			   " UNION ALL " & _
			   " select costguid as projguid from vcb_cost e " & _
			   " INNER JOIN p_CwjkProject f ON f.ProjGUID = e.projguid AND f.CwztGUID = '" & oid & "' " & _
			   " UNION ALL " & _
			   " SELECT productguid  as projguid FROM cb_Product g " & _
			   " INNER JOIN p_CwjkProject h ON h.ProjGUID = g.ProjGUID AND h.CwztGUID = '" & oid & "' " & _
			   " UNION ALL " & _
			   " SELECT projguid FROM p_CwjkProject WHERE CwztGUID = '" & oid & "'")
			nodes = xml.SelectNodes("/Cwzt/Hsxms/Hsxm[@OperObject='合同' or @OperObject='成本科目' or @OperObject='项目名称' or @OperObject='产品']")

		End If
		'售楼房间，楼栋、房间、预约单，成本合同，科目，项目核算GUID
		Dim strHsTypeGuid As String
		For Each node As XmlNode In nodes
			strHsTypeGuid = node.Attributes("HsTypeGUID").Value.ToUpper()
			hashTableHsFiltrate.Add(strHsTypeGuid, strHsTypeGuid)
		Next

		'添加过滤对像GUID
		Dim strProjGuid As String
		For Each row As DataRow In dtTemp.Rows
			strProjGuid = row.Item("projguid").ToString().ToUpper()
			hashTableFiltrate.Add(strProjGuid, strProjGuid)
		Next
		SaveCwjkHsxmMap(xml, oid, strBUGUID, strSQL, hashTableFiltrate, hashTableHsFiltrate, 1)
	End Function

	'引入会计科目2业务数据信息
	Public Shared Function ImportKjkm2OperData(ByVal xml As XmlDocument, ByVal oid As String, ByVal strBUGUID As String, ByVal strApplication As String, ByVal strSQL As StringBuilder)
		Dim dtTemp As DataTable
		Dim hashTableFiltrate As Hashtable = New Hashtable
		Dim hashTableHsFiltrate As Hashtable = New Hashtable
		Dim nodes As XmlNodeList

		'售楼系统过滤项目、楼栋
		If strApplication = "0101" Then
			dtTemp = MyDB.GetDataTable("SELECT a.BldGUID AS ProjGUID FROM p_Building a " & _
			   " INNER JOIN p_CwjkProject b ON a.ProjGUID = b.ProjGUID AND b.CwztGUID = '" & oid & "' " & _
			   " UNION ALL " & _
			   " SELECT g.ProjGUID  AS ProjGUID FROM p_CwjkProject g WHERE CwztGUID = '" & oid & "'")
			nodes = xml.SelectNodes("/Cwzt/Kjkms/Kjkm[@ObjectData='楼栋名称' or @ObjectData='项目名称']")

		Else	'成本过滤项目、合同、科目、产品
			dtTemp = MyDB.GetDataTable(" SELECT contractguid AS projguid FROM  cb_Contract WHERE projtype in ('单项目','多项目') and contractguid in (  " & _
			  " select contractguid from  cb_ContractProj  a INNER JOIN p_CwjkProject  b ON a.ProjGUID = b.ProjGUID AND b.CwztGUID = '" & oid & "' ) " & _
			  " UNION ALL " & _
			  " SELECT  contractGUID AS projguid  from cb_contract c " & _
			  " INNER JOIN p_CwjkProject d ON d.ProjGUID = c.BUGUID AND d.CwztGUID = '" & oid & "' " & _
			  " where projtype in('公司库存','无项目','零星采购合同') " & _
			  " UNION ALL " & _
			  " select costguid as projguid from vcb_cost e " & _
			  " INNER JOIN p_CwjkProject f ON f.ProjGUID = e.projguid AND f.CwztGUID = '" & oid & "' " & _
			  " UNION ALL " & _
			  " SELECT productguid  as projguid FROM cb_Product g " & _
			  " INNER JOIN p_CwjkProject h ON h.ProjGUID = g.ProjGUID AND h.CwztGUID = '" & oid & "' " & _
			  " UNION ALL " & _
			  " SELECT projguid FROM p_CwjkProject WHERE CwztGUID = '" & oid & "'")
			nodes = xml.SelectNodes("/Cwzt/Kjkms/Kjkm[@ObjectData='合同' or @ObjectData='成本科目' or @ObjectData='项目名称' or @ObjectData='产品']")
		End If
		'售楼房间，楼栋，成本合同，科目，项目会计科目GUID
		Dim strKjkmGuid As String
		For Each node As XmlNode In nodes
			strKjkmGuid = node.Attributes("KjkmGUID").Value.ToUpper()
			hashTableHsFiltrate.Add(strKjkmGuid, strKjkmGuid)
		Next

		'添加过滤对像GUID
		Dim strProjGuid As String
		For Each row As DataRow In dtTemp.Rows
			strProjGuid = row.Item("projguid").ToString().ToUpper()
			hashTableFiltrate.Add(strProjGuid, strProjGuid)
		Next
		SaveKjkm2OperData(xml, oid, strBUGUID, strSQL, hashTableFiltrate, hashTableHsFiltrate, 1)
	End Function

	'导入前删除各表数据
	Public Shared Function DeleteInfo(ByVal oid As String, ByRef strSQL As StringBuilder) As String
		strSQL.Append("DELETE FROM p_CwjkKjkm2OperData WHERE CwztGUID='" & oid & "';")
		strSQL.Append("DELETE FROM p_CwjkKjkm2Hsxm WHERE CwztGUID = '" & oid & "';")
		strSQL.Append("DELETE FROM p_CwjkKjkm WHERE CwztGUID='" & oid & "';")
		strSQL.Append("DELETE FROM p_CwjkHsxmMap WHERE CwztGUID = '" & oid & "';")
		strSQL.Append("DELETE FROM p_CwjkHsxm WHERE CwztGUID='" & oid & "';")
		strSQL.Append("DELETE FROM p_CwjkPzEntryRule WHERE CwztGUID ='" & oid & "';")
		strSQL.Append("DELETE FROM p_CwjkOperType WHERE CwztGUID = '" & oid & "';")
		Return ""
	End Function
#End Region

#Region "其它公共、字符处理、解压缩"
	'XML中特殊字符解码
	Public Shared Function DecodeChar(ByVal str As String) As String
		str = str.Replace("&lt;", "<")
		str = str.Replace("&gt;", ">")
		str = str.Replace("&apos;", "'")
		str = str.Replace("&quot;", """")
		str = str.Replace("&amp;", "&")
		Return str
	End Function
	'XML中特殊字符编码
	Public Shared Function CodingChar(ByVal str As String) As String
		str = str.Replace("<", "&lt;")
		str = str.Replace(">", "&gt;")
		str = str.Replace("'", "&apos;")
		str = str.Replace("""", "&quot;")
		str = str.Replace("&", "&amp;")
		Return str
	End Function


	'功能：从解码包中解压指定的文件
	'strpath    --解压后文件路径
	'strFileUrl --被解压文件路径
	'strRarFileName       --解压后的文件名
	'返回：已解压字符串
	Public Shared Function Decode(ByVal strpath As String, ByVal strFileUrl As String, ByVal strRarFileName As String) As Boolean
		'读取压缩包文件
		Dim zStream As ZipInputStream
		Try
			Dim i As Integer
			Dim bytes() As Byte
			strpath = HttpContext.Current.Server.MapPath(strpath)
			strFileUrl = HttpContext.Current.Server.MapPath(strFileUrl)
			Dim fs As New FileStream(strFileUrl, FileMode.Open, FileAccess.Read)
			ReDim bytes(fs.Length - 1)
			fs.Read(bytes, 0, fs.Length - 1)			'数据流读入数组
			fs.Close()

			Dim inputStream As New MemoryStream(bytes)
			zStream = New ZipInputStream(inputStream)						  '压缩流
			Dim objEntry As ZipEntry												'压缩流中文件接口
			Dim ReturnBytes() As Byte

			objEntry = zStream.GetNextEntry()										'取压缩流中的下一个文件
			While Not IsNothing(objEntry)
				Dim strCreateFile As String
				strCreateFile = strpath + strRarFileName

				If Not Directory.Exists(strpath) Then
					Directory.CreateDirectory(strpath)
				End If

				If File.Exists(strCreateFile) Then
					File.Delete(strCreateFile)
				End If
				Dim fs1 As FileStream = File.Create(strCreateFile)	  '建立文件

				If objEntry.Size > 0 Then
					If objEntry.Name.ToLower = "账套设置数据文件.xml" Then
						ReDim ReturnBytes(objEntry.Size - 1)
						For i = 0 To ReturnBytes.Length - 1
							ReturnBytes(i) = zStream.ReadByte
						Next
						fs1.Write(ReturnBytes, 0, ReturnBytes.Length)	   '往文件中写入
						fs1.Close()
						'找到了并解压了账套设置数据文件.xml文件，跳出循环
						zStream.Close()
						Return True
					End If
				Else
					fs1.Close()
				End If
				objEntry = zStream.GetNextEntry()
			End While
		Catch ex As Exception
			Return False
		End Try
		Try
			zStream.Close()
		Catch ex As Exception
		End Try
		Return True
	End Function
    '功能：从解码包中解压指定的文件
    'strpath    --解压后文件路径
    'strFileUrl --被解压文件路径
    'strRarFileName       --解压后的文件名
    '返回：已解压字符串
    Public Shared Function Decode2(ByVal strpath As String, ByVal strFileUrl As String, ByVal strRarFileName As String) As String
        '读取压缩包文件
        Dim zStream As ZipInputStream
        Try
            Dim i As Integer
            Dim bytes() As Byte
            strpath = HttpContext.Current.Server.MapPath(strpath)
            strFileUrl = HttpContext.Current.Server.MapPath(strFileUrl)
            Dim fs As New FileStream(strFileUrl, FileMode.Open, FileAccess.Read)
            ReDim bytes(fs.Length - 1)
            fs.Read(bytes, 0, fs.Length - 1)            '数据流读入数组
            fs.Close()

            Dim inputStream As New MemoryStream(bytes)
            zStream = New ZipInputStream(inputStream)                         '压缩流
            Dim objEntry As ZipEntry                                                '压缩流中文件接口
            Dim ReturnBytes() As Byte

            objEntry = zStream.GetNextEntry()                                       '取压缩流中的下一个文件
            While Not IsNothing(objEntry)
                Dim strCreateFile As String
                strCreateFile = strpath + strRarFileName

                If Not Directory.Exists(strpath) Then
                    Directory.CreateDirectory(strpath)
                End If

                If File.Exists(strCreateFile) Then
                    File.Delete(strCreateFile)
                End If
                Dim fs1 As FileStream = File.Create(strCreateFile)    '建立文件

                If objEntry.Size > 0 Then
                    If objEntry.Name.ToLower = "账套设置数据文件.xml" Then
                        ReDim ReturnBytes(objEntry.Size - 1)
                        For i = 0 To ReturnBytes.Length - 1
                            ReturnBytes(i) = zStream.ReadByte
                        Next
                        fs1.Write(ReturnBytes, 0, ReturnBytes.Length)      '往文件中写入
                        fs1.Close()
                        '找到了并解压了账套设置数据文件.xml文件，跳出循环
                        zStream.Close()
                        Return ""
                    End If
                Else
                    fs1.Close()
                End If
                objEntry = zStream.GetNextEntry()
            End While
            Return "您选择的文件不是系统导出的账套文件！"
        Catch ex As Exception
            Return "您选择的文件不是系统导出的账套文件！"
        End Try
        Try
            zStream.Close()
        Catch ex As Exception
        End Try
        Return ""
    End Function


	'生成zip文件
	Public Shared Function CreateZip(ByVal strPath As String, ByVal strZipFileFullName As String) As Boolean
		'读取压缩包文件
		Dim iBs As Integer = 0
		Dim arrFiles() As String = IO.Directory.GetFiles(strPath)
		Dim strFiles As String

		Dim i As Integer
		Dim objCrc32 As New Crc32
		Dim strmZipOutputStream As ZipOutputStream
		Dim fs As FileStream

		'压缩数据流
		strmZipOutputStream = New ZipOutputStream(File.Create(strZipFileFullName))

		Try
			For i = 0 To arrFiles.Length - 1		'循环压进压缩包
				If File.Exists(arrFiles(i)) Then
					Dim bytes() As Byte
					fs = New FileStream(arrFiles(i), FileMode.Open, FileAccess.Read)

					ReDim bytes(fs.Length - 1)
					fs.Read(bytes, 0, fs.Length)			'数据流读入数组
					fs.Close()

					'设置压缩率     0 -- 不压缩
					'               9 -- 最大压缩比
					strmZipOutputStream.SetLevel(9)
					'被压缩文件接口信息
					Dim objZipEntry As ZipEntry = New ZipEntry(Path.GetFileName(arrFiles(i)))
					'存放在压缩文件中的文件名
					objZipEntry.DateTime = DateTime.Now							'压缩时间
					objZipEntry.Size = bytes.GetLength(0)					'被压缩文件大小
					objCrc32.Reset()
					objCrc32.Update(bytes)
					objZipEntry.Crc = objCrc32.Value							'冗余校验值

					strmZipOutputStream.PutNextEntry(objZipEntry)
					strmZipOutputStream.Write(bytes, 0, bytes.Length)
					iBs = iBs + 1

					'删除xml文件
					File.Delete(arrFiles(i))
				End If

			Next
		Catch ex As Exception
			'fs.Close()

			Return ex.ToString()

		End Try

		'关闭
		strmZipOutputStream.Finish()
		strmZipOutputStream.Close()
		If iBs <> 0 Then
			Return True
		Else
			Return False
		End If

	End Function

#End Region

End Class
